[HowTo] Install a vnc server on the Raspberry pi

Update 2021 (November)
@MarkusLange, has updated his script (check the 2019 update bellow for the link and instructions) to take care of the KMS/fKMS problem with the Pi4 if you are using Kodi version >=19.1, as seen here: [HowTo] Install a vnc server on the Raspberry pi - #482 by MarkusLange
So just use his script to install vnc server on your pi…It is safer than doing it manually.

Update 2021 (September)
In order to make it work on the Pi4, after installation do the following:

Update 2019
@MarkusLange has created an easy installer to setup a vnc server on a raspberry pi.
You can use his guide here: [HowTo] Install a vnc server on the Raspberry pi - #394 by MarkusLange to install it using an installer. It’s pretty simple just follow the instructions and select “Install VNC Server and Service” or use the console command to also setup a port, framerate and password in one line like so:
sudo ./osmc_vnc_install_cli.bash --install-vnc 1234 30 osmc

Instructions for manual Installation

Step 1: Based on @patrikolausson 's instructions on github GitHub - patrikolausson/dispmanx_vnc: VNC Server for Raspberry PI using dispmanx you have to install all the necessary packages first in order to compile the vnc server.

sudo apt-get install build-essential rbp-userland-dev-osmc libvncserver-dev libconfig++-dev unzip
cd /home/osmc
sudo wget https://github.com/patrikolausson/dispmanx_vnc/archive/master.zip
unzip master.zip -d  /home/osmc/
rm master.zip
cd dispmanx_vnc-master
make

Step 2: To add the vnc server as a service, (so it loads automatically when the pi reboots), do the following:

First of all lets create a basic config file (password, fps etc) and copy the server to the /bin folder for easy execution.
We’ll go inside the dispmanx_vncserver folder we just compiled using patrik’s method, and do

sudo cp dispmanx_vncserver /usr/bin
sudo chmod +x /usr/bin/dispmanx_vncserver
sudo cp dispmanx_vncserver.conf.sample /etc/dispmanx_vncserver.conf
sudo vi /etc/dispmanx_vncserver.conf

(You can use nano if you want instead of vi)…
Press i to activate write mode in vi and then copy paste this inside and change the password inside the quotes:

relative = false;
port = 0;
screen = 0;
unsafe = false;
fullscreen = false;
multi-threaded = false;
password = "mypassword";
frame-rate = 23;
downscale = false;
localhost = false;
vnc-params = "";

Press ESC, then type :wq to save the changes you made and quit vi.


Notes:
Some people might have a problem using port = 0; in the above config file.
If you also have problems connecting to your pi, then the only solution is to do port forwarding, so change that line to port = 5900;
Instructions on how to forward port 5900 to your pi can be found here: http://portforward.com/english/applications/port_forwarding/RealVNC/default.htm
If you enable port 5900 then make sure you have set a static ip on your pi’s network settings. Also please note that this will allow access to your pi from outside your LAN network…so don’t put 12345 or something easy for your vnc password…you might get hacked…

Also, notice the downscale and multi-threaded values ? These 2 values when set to true could help out to have a better feedback from the vncserver. The first one downscales the screen to a quarter in vnc and the other runs vnc in a separate thread. Try them out,


Now we’ll create the service file to be able to ‘autoload’ the server on boot:

sudo vi /etc/systemd/system/dispmanx_vncserver.service

then press i again to activate writing inside the editor, and paste this inside:

[Unit]
Description=VNC Server
After=network-online.target mediacenter.service
Requires=mediacenter.service

[Service]
Restart=on-failure
RestartSec=30
Nice=15
User=root
Group=root
Type=simple
ExecStartPre=/sbin/modprobe evdev
ExecStart=/usr/bin/dispmanx_vncserver
KillMode=process

[Install]
WantedBy=multi-user.target

Save and quit.
After that start the service and make it load or reboot

sudo systemctl start dispmanx_vncserver.service
sudo systemctl enable dispmanx_vncserver.service
sudo systemctl daemon-reload

That should do it :slight_smile: You can now connect to your pi from your PC using your pi’s local ip address.
If you used port 5900 method, then also add :5900 at the end of the address.


For MacOS users:


Final Note:
If you dont want the server to load on boot, you can make a shortcut on your remote control to start/stop the server. Further info here:

Additional note:
If for some reason you need to remove the service do this:

sudo systemctl stop dispmanx_vncserver.service
sudo systemctl disable dispmanx_vncserver.service
sudo systemctl daemon-reload

End of Guide

=========================================================================================

(This method was left here in case someone wants to do this the ‘hard way’ and not use patrik’s github method)
Old instructions on how to setup a vnc server…Deprecated method…(dont use it)

I’ve managed to run vncserver from here: GitHub - hanzelpeter/dispmanx_vnc: VNC Server for Raspberry PI using dispmanx
And this is how:

Install dependencies

sudo apt-get update
sudo apt-get install libvncserver-dev
sudo apt-get install rbp-userland-dev-osmc
sudo apt-get install gcc

Get the files needed and compile the server
Type in console:

wget https://github.com/hanzelpeter/dispmanx_vnc/archive/master.zip
unzip master.zip -d  /home/osmc/
rm master.zip

–Optional–
If you want to make the vnc server a bit faster, then edit main.c inside the dispmanx_vnc-master folder and search for:
#define PICTURE_TIMEOUT (1.0/15.0)
change it to:
#define PICTURE_TIMEOUT (1.0/25.0)
-----------

Compile the file

cd /home/osmc/dispmanx_vnc-master/
sudo chmod +x makeit
./makeit

Start the server

sudo modprobe uinput
sudo chmod 666 /dev/uinput
./dispman_vncserver

(Ctrl + C to stop it)

Log in from your PC with vnc viewer
END

If you want to make the file executable from anywhere then:

sudo cp dispman_vncserver /usr/bin
chmod +x /usr/bin/dispman_vncserver
16 Likes

You should instead look at apt-get install rbp-userland-dev-osmc for the development headers. These will match the userland libraries that we ship in OSMC

Sam

Thanks Sam,
i figured that osmc would have something like that, i just didnt know how to get them.

Sorry for the dumb question but I am new to linux… Is it possible to run this vnc server simply by “dispman_vncserver” instead of “cd dispmanx_vnc/” and then “./dispman_vncserver”.

Thanks

yeah, just copy dispman_vncserver in folder /usr/bin and then you’ll be able to execute it from anywhere.

And here is a way to bind vncserver to a key in your remote control and toggle it on/off by pressing a button. (at the bottom)

discourse.osmc.tv/t/built-in-functions-for-skins/1520/4

I have it bind to the button “win”. You’ll have to find the proper ID for the key you want to use.
The vncboot script is just a simple bash script. Just go to folder /etc/init.d/ or wherever you want really…and type:

nano vncboot

Then copy paste the script Ctrl+X and save…(if you dont place it in another folder dont forget to change the path in the python script as well)

(The above script needs dispman to be at /usr/bin/dispman_vncserver)

Thanks, I copied the dispman_vncserver to /usr/bin but I cant run it in ssh even if I am root. It says
-bash: /usr/bin/dispman_vncserver: Permission denied

I found a solution for this problem http://stackoverflow.com/a/18960752 but it did not worked. Still the same error

try

sudo dispman_vncserver

its because you are not using the root account to execute it but the osmc one.
Another solution would be to get root access and give rights to the file to be executed by the osmc user

sudo passwd root

will allow you to set the root password.

You are probably using an FTP type of client like winscp to upload files to the pi right? If you use winscp then log in with your new root account, and right click/properties the dispman_vncserver file and change owner and group to osmc.
Not sure if you will also need to do the +x thing after, but do it anyways.:

chmod +x /usr/bin/dispman_vncserver

After that you should be able to execute the file as osmc as well.

Also a final note…install screen:

apt-get install screen

It allows you to have more than one sessions in your console window… and can come back to it after if you want (for example you start the vncserver, shutdown your PC, log back the next day and continue your console session).
Some info and keyboard shorcuts to go back and forward with screen:
https://www.howtoforge.com/linux_screen

ps: not sure if i have screen on my pi, (its currently offline)…but i’ve been playing with another linux distro today and i’ve been using it a lot. I hope pi has it :stuck_out_tongue:

EDIT: I just read your post correctly…you said “even if i am root” oO ?!
It still doesnt work with root ? hmmm
Try this as well:

chmod 755 /usr/bin/dispman_vncserver

Thanks that last command worked without changing the owner etc.

Could you please explain the Screen package more? It means, console keeps its state even if I close it and shut down the pc? How can I then close the console, if I want to?

For example, you are doing some stuff there in the console…like having htop running 24/7 or running a python script or whatever…remotely from your PC on the pi, using putty.
If you close that console…then the session ends in the pi…you cant go back to it.
But if you have screen installed you can close the console…and when you come back, you attach the screen ID again and continue from where you left it.
For example:
I log in the pi with putty. I type screen and then enter to close it.
Then i execute htop and the program pops up (its an advanced process display app. you can get it with : apt-get install htop).
Then i close my console…go to sleep, and the next day i log back in again.
Then i type : screen -ls and it show for example : 2634.session.
I type : screen -r 2634 and it brings back my htop process that i left yesterday…as if i never closed the console.

You probably wont need screen at all if you dont do stuff with the pi console. But if you are like me :stuck_out_tongue: probably in the future you’ll start playing with scripts, packages etc in the pi’s linux. Screen is an app that helps alot, when doing things remotely.

1 Like

Actually I am doing everything in the console (however only 2nd day so I dont know much yet), so this might be useful. Thanks

Yeah its really useful having a linux distro operating 24/7.
I got a .tk domain name DNS updater running on my pi, so its updates my dynamic ip every hour, if it ever changes.

Some stuff to do with the pi: Results for raspberry | Hackaday.io
And if you ever want a touch screen: -=Link1=- -=Link2=-
I am waiting for the 2nd one to come…(i assumed v4 is better than v3…most people have v3 though)

1 Like

Thanks for the link, it is great resource. However I think we are a bit off topic :smiley:

Back to topic - this tutorial is working great on RC :smile: Just update 3rd point as it is unnecessary since there is apt-get install rbp-userland-dev-osmc

You tested it and worked ? I’ll be reinstalling osmc when noobs has it, so i get raspbian as well.
Thought to try it then again…but if it works, i’ll change it from now…just need a confirmation :slight_smile:

Yes it works

1 Like

Thanks to everyone for their contribution.
I changed the 1st post.

Great, it works as expected. To skip the “download to pc and move the files using winscp”, what I did was to replace that part with

wget https://github.com/hanzelpeter/dispmanx_vnc/archive/master.zip

and then

unzip master.zip -d  dispmanx_vnc/

(having dispmanx_vnc already manually created as a folder)

1 Like

Thanks updated the 1st post and added some extra stuff at the bottom.

quick note in case some of the users trying to follow the guide get some errors, the unzip actually also creates a subfolder inside our initial one, called dispmanx_vnc-master, so our path actually becomes

/home/osmc/dispmanx_vnc/dispmanx_vnc-master/

we could skip creating dispmanx_vnc and just rely on what’s inside the archive, and update the cd to point to that, your call :slight_smile:

1 Like

fixed the 1st post…good catch :slight_smile: