1

I am a beginner who just installed ubuntu 18.04 just fews days ago, and I am currently studying how to do everything from the terminal.

Chrome is a necessary application for me, for example, and I have already known how to run it by using the command line like/opt/google/chrome/chrome, which is the default path to chrome. But for Firefox, which was installed initially, I just need to type firefox and the browser works just fine.

And now I'm wondering if I can open Chrome in a similar way rather than typing the complicated path. Anyone has ideas?

Ruxu
  • 29

4 Answers4

4

TL;DR

For deb-packaged Google Chrome you can simply use google-chrome (or google-chrome-stable) command in the terminal.

Details

On my system I have stable version of Google Chrome 80 installed.
Its package name is google-chrome-stable, its binary files may be found by using

$ dpkg --listfiles google-chrome-stable | grep bin/
/usr/bin/google-chrome-stable

but really this file is a symlink to the location which you have mentioned (/opt/google/chrome/google-chrome):

$ ls -al /usr/bin/google-chrome-stable
lrwxrwxrwx 1 root root 32 Feb 13 07:04 /usr/bin/google-chrome-stable -> /opt/google/chrome/google-chrome

The /usr/bin is usually added to the PATH variable by default. So it is not really needed to add /opt/google/chrome/google-chrome to the PATH by hand.

For more complicated applications and their desktop- and executable files you can use methods from my other answer.

N0rbert
  • 99,918
4

There are different ways to customize the name by which you can launch an application:

Method 1 - Create an alias Next to all options given in the other answers, a convenient way to make it easier for you to start Chrome from the command line, and which does not require to be administrator, is to create an alias:

alias chrome=/opt/google/chrome/chrome

Add this command to your .bashrc file (already, some other aliases are defined there), so it is available anytime.

Method 2 - Create a symlink also does not require root permissions: you can create your personal bin folder in your home directory. There, create a symbolic link to the executable of Google Chrome as:

ln -s /usr/bin/google-chrome ~/bin/chrome

On Ubuntu, a local bin folder is automatically added to the PATH. After creating the bin folder, it will take only effect after your next log in.

Method 3 - Create a systemwide symlink If the change is to affect all users, create an appropriately named symlink in /usr/bin. As mentioned in the other answers, a symlink may already be available. If you prefer the shorter name chrome, feel free to create an additional symlink as

sudo ln -s /usr/bin/google-chrome /usr/bin/chrome

or rename the existing symlink.

Method 4 - Create a wrapper script. A simple executable bash script placed in your local bin folder or in urs/bin or another folder in your path would, for practical purposes, equally work. When executed, the script in turn calls the application by specifying its full pathway. A wrapper script is particularly suited if there is a need to customize the working environment prior to launching the application. Thus, the script could change the working directory, set or change environment variables, etc.

vanadium
  • 88,010
  • To be thorough there is a 4th method as posted by christian: adding the path to $PATH – karlsebal Feb 15 '20 at 11:51
  • 1
    @karlsebal, agree this is a method, but it is not the best. One should not have to add an entry to the PATH for each individual application. – vanadium Feb 15 '20 at 14:09
  • hash -r rereads the $PATH so bash knows the new contents of ~/bin – Thorbjørn Ravn Andersen Feb 15 '20 at 15:13
  • chrome and chromium are distinct builds; I wouldn't suggest using one name as an alias for the other. – Peter Cordes Feb 15 '20 at 16:06
  • @PeterCordes For sure it should be "chrome=". Thank you, I have corrected this. – vanadium Feb 15 '20 at 16:32
  • @vanadium agreed. – karlsebal Feb 15 '20 at 17:00
  • @vanadium: I disagree. Adding the path to your $PATH is the standard (and IMHO best) way. Especially if the "application" has multiple executables. – jamesqf Feb 15 '20 at 17:12
  • @jamesqf "The best" is arguable. If it is for you, then it is OK. It is an intrusive system change, and not standard practice. For example, Imagemagicks convert has symbolic links to all its diverse utilities in /usr/bin/. It could instead have added its own path. Oh, how I long for the former days in MS Windows where every application would find it useful to add itself to the PATH, and of course on the front.... We had nice large paths back then... Excuse the sarcasm. – vanadium Feb 16 '20 at 11:11
  • @vanadium: Sure, cases differ. But using $PATH handles some cases that symlinks in /usr/bin doesn't. For instance, I have two implementations of MPI (MPICH and OpenMPI) that I sometimes need to test things with. Their commands have the same name, so I can't just have symlinks. Changing $PATH so that it includes whichever one I want to use solves the problem. – jamesqf Feb 17 '20 at 17:06
  • @jamesqf That is already a very specific scenario you are dealing with, very atypical for the common user. Of course, you may then want to change the PATH to change your testing environment. An equally valid alternative, though, would be to change the symlink to point to one or the other version. That symlink can even reside in your ~/bin so you also do not need to be root. – vanadium Feb 17 '20 at 17:39
  • @vanadium: Changing symlinks requires knowing what all the symlinks are, and (unless you're into lots of typing) writing a script to do it. Much easier to just change your $PATH. And of course entries in $PATH can point to ~/bin, or anywhere else you like. It can, at least in my experience, do things easily that are otherwise difficult. I certainly admit that I don't know that I'm not a "common user", but I do see plenty of "common user" things that can be done better/easier by expert users :-) – jamesqf Feb 18 '20 at 17:45
  • @jamesqf Fully agree for changing testing environments, but I do not at all agree that permanently adapting your path would be an easier/better way for installing user applications. – vanadium Feb 19 '20 at 12:49
  • @vanadium: I'd agree that a user-level package like chrome, firefox, or ImageMagick ought to handle this behind the scenes, whether it's via symlinks or some other method. – jamesqf Feb 20 '20 at 03:51
  • @jamesqf a well behaved app does that with symlinks or a wrapper script, not by changing the user's environment. – vanadium Feb 20 '20 at 08:39
  • @vanadium: But we're discussing what a USER can do, not what the authors of a packaged app should do. For a user, changing the path is generally the easiest and IMHO best way. – jamesqf Feb 21 '20 at 03:15
  • Back in Windows 3.1, this was common practice. – vanadium Feb 21 '20 at 09:57
1

export PATH=$PATH:/opt/google/chrome/chrome

This will allow you to type chrome in your terminal to launch it.

This works by setting your path variable to itself and then appending :/opt/google/chrome/chrome

  • thanks a lot! i think this statement should work by putting it into the profile, like Christian said. Anyway, thanks for your help. – Ruxu Feb 15 '20 at 04:41
  • 2
    This is not really needed as /usr/bin/google-chrome-stable and /opt/google/chrome/google-chrome are symlinks, while /usr/bin is already in PATH. – N0rbert Feb 15 '20 at 10:08
1

This is what the $PATH variable is for. When you're able to call firefox (or gedit, or whatever) by name, without referencing an absolute path, that's usually because it's in /usr/bin/ and /usr/bin/ is in your $PATH by default.

You can add an export statement to your .profile (so it's persistent) and reload it with:

echo 'export PATH=$PATH:/opt/google/chrome' >> ~/.profile
source ~/.profile

And then you can call Chrome with the simple chrome command in terminal. You can also go to Settings > Keyboard Shortcuts and set up a keyboard shortcut so you can open it without the terminal at all, or even set it as a startup application so it opens by default on login.

Lewis
  • 49
  • This is not really needed as /usr/bin/google-chrome-stable and /opt/google/chrome/google-chrome are symlinks, while /usr/bin is already in PATH. – N0rbert Feb 15 '20 at 10:07
  • this depends on the installation method. The symlink in /usr/bin is not guaranteed to be there. – karlsebal Feb 15 '20 at 11:48