1

If I create the following program in C:

#include <stdio.h>

int main(){
    printf("Hello World");
}

And then compile it like so:

gcc helloworld.c -o helloworld

Is there any easy way to install helloworld on to my computer?

  • 2
    ehm it is already on your computer ;-) Next step: make it executable (chmod 664 helloworld) and then you can execute it with ./helloworld. – Rinzwind Nov 2 '15 at 10:43
  • But is there any way to install it so I can type helloworld into the terminal from any directory @Rinzwind – dangee1705 Nov 2 '15 at 10:47
  • 1
    That is not "installing" but copying it into a directory that is included in your PATH Something like sudo cp helloworld /usr/local/bin/. After you made it executable ;) – Rinzwind Nov 2 '15 at 10:49
  • @Rinzwind Correct me if I'm wrong, but as far as I can understand from reading this guide on deb packaging , that's kind of what really happens under the hood - the source files are packaged into deb file, once they arrive at a pc , the source is just compiled and executables are shoved into appropriate dirs. Correct ? – Sergiy Kolodyazhnyy Nov 2 '15 at 11:03
3

sudo install helloworld /usr/local/bin installs it to /usr/local/bin (read man install).

Btw, your helloworld.c should really be:

#include <stdio.h>

int main(void){
    printf("Hello World");
    return 0;
}
4

Is there any easy way to install helloworld on to my computer?

The next actions you need to do:

  1. make the file executable with chmod 775 helloworld from the directory where the file is.
  2. copy it over to a directory in your PATH. I would suggest sudo cp helloworld /usr/local/bin/ (local since it is your local system and bin since it is a binairy).

And then you can do

helloworld

from any location on the system to have it print "Hello world".

  • 2
    Why 664? It'll make it unexcutable, 755 should be used. And gcc already makes it executable by default. – cylgalad Nov 2 '15 at 12:25
3

Without root permission the best thing for you to do is create a bin directory within your home directory and then add it to your PATH environment variable. So after creating a directory called bin in /home/$USER open ~/.bashrc in your favourite editor, for example gedit ~/.bashrc. You then need to add:

export PATH=$PATH:~/bin

What this is doing is appending ~/bin to the $PATH variable. Which is a list of directories to search for executables.

If you've got a terminal currently open, you need to reload the configuration what you can do by using the source command: source ~/.bashrc.

Finally you just need to copy the executable into that directory then you'll be able to run it as you would a normal command.

As pointed out by @Serg in the comments the ~/bin directory is included in the PATH set in ~/.profile. So alternatively you can simply edit ~/.profile and remove the comment from the PATH variable. Simply find the line:

# PATH="$HOME/bin:$PATH"

Then change it to the following and reload the terminal:

PATH="$HOME/bin:$PATH"
  • 2
    1 thing: this will make it possible to execute if from only this user you did this for. If you need it for more than 1 user use a global directory (like /usr/local/bin And yes that requires "sudo" permissions). – Rinzwind Nov 2 '15 at 10:53
  • Yes, if you require it to be executable for multiple users then @Rinzwind answer is the way to do it. It has the benefit of not needing to modify the PATH – AJefferiss Nov 2 '15 at 10:55
  • A side note: .profile actually contains the following line # PATH="$HOME/bin:$PATH" , so the user alternatively could just uncomment that in their .profile file – Sergiy Kolodyazhnyy Nov 2 '15 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.